

//题1
async function async1() {
    console.log(1)
    await async2()//微任务1
    console.log(2)
}

async function async2() {
    console.log(3)
}

console.log(4)

async1()

setTimeout(function () {//宏任务1
    console.log(5)
}, 0)

new Promise(resolve => {
    console.log(6)
    resolve()
}).then(function () {//微任务2
    console.log(7)
}).then(function () {//微任务3
    console.log(8)
})

console.log(9)

// 同步
// 4
// 1
// 3
// 6
// 9

// 异步
// 2
// 7
// 8
// 5

//题2
async function async1() {
    console.log('async1 start');
    let a = await async2()//微任务1
    console.log(a)
    console.log('async1 end')
}
async function async2() {
    console.log('async2')
    return 'async2 return'
}

console.log('script start')
setTimeout(() => {//宏任务1
    console.log('settimeout')
}, 0);
async1()
new Promise(function (resolve) {
    console.log('promise1')
    resolve()
}).then(function () {//微任务2
    console.log('promise2')
})
console.log('script end')

// 同步任务：
// script start
// async1 start
// async2--------------
// promise1
// script end

// 异步任务：
// async2 return
// async1 end
// promise2-------------
// settimeout


//题3
// setImmediate---立即异步执行一个任务---宏任务
// process.nextTick---立即异步执行一个任务---微任务

setImmediate(function() {//h-1
    console.log('immediate1');
    setImmediate(function() {//h-5
        console.log('immediate2');
    })
    process.nextTick(function() {//w-3
        console.log(`process1`);
    })
})

process.nextTick(function() {//w-1
    console.log(`process2`)
    setImmediate(function() {//h-3
        console.log('immediate3');
        process.nextTick(function() {
        console.log(`process3`);
        })
    })
})

process.nextTick(function() {//w-2
    console.log(`process4`)
    setImmediate(function() {//h-4
        console.log('immediate4');
        process.nextTick(function() {
        console.log(`process5`);
        })
    })
})

setImmediate(function() {//h-2
    console.log('immediate5');
    process.nextTick(function() {//w-4
        console.log(`process6`);
    })
})

console.log(`window`)

//   window

//   process2
//   process4
//   immediate1
//   process1
// immediate5
// process6
// immediate3
// process3
// immediate4
// process5
// immediate2